# setup-mcp.ps1 - MCP 服务自动配置脚本
# 版本: 1.0.0
# 用途: 在 Windows 11 新电脑上自动配置 MCP 服务

Write-Host "=== MCP 服务配置脚本 ===" -ForegroundColor Cyan
Write-Host "版本: 1.0.0" -ForegroundColor Gray
Write-Host ""

# 1. 检查 Node.js
Write-Host "[1/6] 检查 Node.js..." -ForegroundColor Yellow
try {
    $nodeVersion = node --version 2>&1
    if ($LASTEXITCODE -ne 0) {
        throw "Node.js 未安装"
    }
    Write-Host "✓ Node.js 版本: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "❌ Node.js 未安装，请先安装: https://nodejs.org/" -ForegroundColor Red
    Write-Host "   推荐安装 LTS 版本 (>= 20.10.0)" -ForegroundColor Yellow
    pause
    exit 1
}

# 2. 检查 npm
Write-Host "`n[2/6] 检查 npm..." -ForegroundColor Yellow
try {
    $npmVersion = npm --version 2>&1
    Write-Host "✓ npm 版本: $npmVersion" -ForegroundColor Green
} catch {
    Write-Host "❌ npm 不可用" -ForegroundColor Red
    pause
    exit 1
}

# 3. 安装 Playwright
Write-Host "`n[3/6] 安装 Playwright..." -ForegroundColor Yellow
Write-Host "   这可能需要几分钟时间，请耐心等待..." -ForegroundColor Gray

try {
    npm install -g @playwright/test 2>&1 | Out-Null
    npx playwright install chromium 2>&1 | Out-Null
    Write-Host "✓ Playwright 安装完成" -ForegroundColor Green
} catch {
    Write-Host "⚠ Playwright 安装失败，但不影响其他服务" -ForegroundColor Yellow
    Write-Host "   可以稍后手动安装: npm install -g @playwright/test" -ForegroundColor Gray
}

# 4. 创建配置目录
Write-Host "`n[4/6] 创建配置目录..." -ForegroundColor Yellow
$configDir = "$env:USERPROFILE\.claude"
if (!(Test-Path $configDir)) {
    try {
        New-Item -ItemType Directory -Path $configDir -Force | Out-Null
        Write-Host "✓ 创建目录: $configDir" -ForegroundColor Green
    } catch {
        Write-Host "❌ 创建目录失败: $_" -ForegroundColor Red
        pause
        exit 1
    }
} else {
    Write-Host "✓ 目录已存在: $configDir" -ForegroundColor Green
}

# 5. 备份现有配置
Write-Host "`n[5/6] 备份现有配置..." -ForegroundColor Yellow
$configFile = "$configDir\claude_desktop_config.json"
if (Test-Path $configFile) {
    try {
        $timestamp = Get-Date -Format 'yyyyMMdd_HHmmss'
        $backupFile = "$configFile.backup.$timestamp"
        Copy-Item $configFile $backupFile -Force
        Write-Host "✓ 已备份到: $backupFile" -ForegroundColor Green
    } catch {
        Write-Host "⚠ 备份失败: $_" -ForegroundColor Yellow
        Write-Host "   将继续写入新配置" -ForegroundColor Gray
    }
} else {
    Write-Host "✓ 无需备份（配置文件不存在）" -ForegroundColor Green
}

# 6. 写入配置
Write-Host "`n[6/6] 写入 MCP 配置..." -ForegroundColor Yellow

$config = @{
    mcpServers = @{
        "context7" = @{
            command = "npx"
            args = @("-y", "@upshift-inc/context7")
        }
        "serena" = @{
            command = "npx"
            args = @("-y", "@upshift-inc/serena")
        }
        "skill-seeker" = @{
            command = "npx"
            args = @("-y", "@upshift-inc/skill-seeker")
        }
        "spec-workflow" = @{
            command = "npx"
            args = @("-y", "@upshift-inc/spec-workflow")
        }
        "playwright-mcp" = @{
            command = "npx"
            args = @("-y", "@playwright/mcp")
        }
    }
}

try {
    $jsonContent = $config | ConvertTo-Json -Depth 10
    $jsonContent | Set-Content $configFile -Encoding UTF8 -Force
    Write-Host "✓ 配置已写入: $configFile" -ForegroundColor Green
} catch {
    Write-Host "❌ 写入配置失败: $_" -ForegroundColor Red
    pause
    exit 1
}

# 7. 验证配置
Write-Host "`n[验证] 检查配置文件..." -ForegroundColor Yellow
try {
    $testConfig = Get-Content $configFile -Raw | ConvertFrom-Json
    $serverCount = $testConfig.mcpServers.PSObject.Properties.Count
    Write-Host "✓ 配置文件格式正确，包含 $serverCount 个 MCP 服务" -ForegroundColor Green
} catch {
    Write-Host "⚠ 配置文件格式可能有误: $_" -ForegroundColor Yellow
    Write-Host "   请手动检查: $configFile" -ForegroundColor Gray
}

# 完成提示
Write-Host "`n=== 配置完成 ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "已配置的 MCP 服务:" -ForegroundColor White
Write-Host "  1. context7         - 获取库文档和上下文" -ForegroundColor Gray
Write-Host "  2. serena           - 语义代码分析工具" -ForegroundColor Gray
Write-Host "  3. skill-seeker     - 创建 Claude 自定义技能" -ForegroundColor Gray
Write-Host "  4. spec-workflow    - 功能开发工作流管理" -ForegroundColor Gray
Write-Host "  5. playwright-mcp   - 浏览器自动化测试" -ForegroundColor Gray
Write-Host ""
Write-Host "下一步操作:" -ForegroundColor Yellow
Write-Host "  1. 如需配置 Shadcn Vue MCP Server，请:" -ForegroundColor White
Write-Host "     - 手动安装到本地目录" -ForegroundColor Gray
Write-Host "     - 编辑配置文件添加路径" -ForegroundColor Gray
Write-Host "  2. 重启 Claude Code CLI" -ForegroundColor White
Write-Host "  3. 运行 'claude mcp list' 验证配置" -ForegroundColor White
Write-Host "  4. 测试服务: 'claude mcp test <服务名>'" -ForegroundColor White
Write-Host ""
Write-Host "配置文件位置: $configFile" -ForegroundColor Cyan
Write-Host "完整文档: docs\MCP服务配置完整指南.md" -ForegroundColor Cyan
Write-Host ""
pause
